﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : TianTeng
// Created          : 09-03-2018
//
// Last Modified By : TianTeng
// Last Modified On : 09-03-2018
// ***********************************************************************
// <copyright file="ProjectDbUtil.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using OSS.BIM.Model;

namespace OSS.BIM.Business.Utility
{
    /// <summary>
    /// Class ProjectDbUtil.
    /// </summary>
    public static class ProjectDbUtil
    {
        /// <summary>
        /// Gets at.
        /// </summary>
        /// <param name="projectId">The project identifier.</param>
        /// <returns>Project.</returns>
        public static Project GetAt(long projectId)
        {
            return DB.Context.From<Project>().Where(d => d.ProjectId == projectId).First();
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns>Project[].</returns>
        public static Project[] GetAll()
        {
            return DB.Context.From<Project>().ToList().ToArray();
        }

        /// <summary>
        /// News the identifier if not set.
        /// </summary>
        /// <param name="project">The project.</param>
        public static void NewIdIfNotSet(this Project project)
        {
            if (project.ProjectId == 0)
                project.ProjectId = SnowflakeUtil.GetId();
        }

        /// <summary>
        /// Inserts the specified project.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Insert(Project project)
        {
            project.NewIdIfNotSet();
            return DB.Context.Insert(project) > 0;
        }

        /// <summary>
        /// Gets the last identifier.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public static long GetLastId()
        {
            return DB.Context.From<Project>().Select(d => d.ProjectId)
                .OrderByDescending(Project._.ProjectId).Top(1).First().ProjectId;
        }

        /// <summary>
        /// Updates the specified project.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Update(Project project)
        {
            return DB.Context.Update(project) > 0;
        }

        /// <summary>
        /// Determines whether the specified user project is exit.
        /// </summary>
        /// <param name="userProject">The user project.</param>
        /// <returns>System.Int32.</returns>
        public static int IsExit(this Project userProject)
        {
            return DB.Context.From<Project>().Where(d => d.ProjectId == userProject.ProjectId).Count();
        }

        /// <summary>
        /// Deletes the specified user project.
        /// </summary>
        /// <param name="userProject">The user project.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Delete(Project userProject)
        {
            DB.Context.Delete<ModelGrope>(d => d.ProjectId == userProject.ProjectId);
            if (DB.Context.Delete(userProject) == 0)
            {
                return false;
            }
            return true;
        }
    }
}
